package com.stylefeng.guns.modular.computertype.controller;

import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.persistence.dao.ComputerTypeMapper;
import com.stylefeng.guns.common.persistence.model.ComputerType;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.computertype.dao.ComputertypeDao;
import com.stylefeng.guns.modular.computertype.service.IComputertypeService;
import com.stylefeng.guns.modular.system.warpper.ComputerTypeWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * computertype控制器
 *
 * @author fengshuonan
 * @Date 2019-07-11 16:19:05
 */
@Controller
@RequestMapping("/computertype")
public class ComputertypeController extends BaseController {

    private String PREFIX = "/computertype/computertype/";

    @Resource
    private ComputertypeDao computertypeDao;
    @Resource
    private IComputertypeService computertypeService;
    @Resource
    private ComputerTypeMapper computerTypeMapper;

    /**
     * 跳转到computertype首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "computertype.html";
    }

    /**
     * 跳转到添加computertype
     */
    @RequestMapping("/computertype_add")
    public String computertypeAdd() {
        return PREFIX + "computertype_add.html";
    }

    /**
     * 跳转到修改computertype
     */
    @RequestMapping("/computertype_update/{computertypeId}")
    public String computertypeUpdate(@PathVariable Integer computertypeId, Model model) {
        ComputerType computertype = this.computerTypeMapper.selectById(computertypeId);
        model.addAttribute("computertype", computertype);
        LogObjectHolder.me().set(computertype);
        return PREFIX + "computertype_edit.html";
    }

    /**
     * 获取computertype列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> computerType = this.computertypeService.selectComputerType(condition);
        return super.warpObject(new ComputerTypeWarpper(computerType));
    }

    /**
     * 新增computertype
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ComputerType computerType) {

        return this.computertypeService.addComputerType(computerType);
    }

    /**
     * 删除computertype
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer computertypeId) {
        int flag = computertypeService.isDelete(computertypeId);
        if (flag > 0) {
            return SUCCESS_MESSAGE;
        } else {
            computerTypeMapper.deleteById(computertypeId);
            return SUCCESS_TIP;
        }

    }


    /**
     * 修改computertype
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ComputerType computerType) {
        this.computertypeService.updateComputerType(computerType);
        return super.SUCCESS_TIP;
    }

    /**
     * computertype详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
